﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VMC.Domain.Entity;
using VMC.Domain.Service;
using VMC.Domain.Service.Abstract;
using VMC.Domain.Model.Service.Abstract;
using VMC.Domain.Model.DataStructure;
using VMC.Domain.Model.Common;

namespace VMC.Domain.Model.Service
{
    public class ComplexDataTypeColumnModelService : ModelService, IComplexDataTypeColumnModelService
    {
        private IComplexDataTypeColumnService iColumns;
        private IComplexDataTypeColumnMappingService iMapping;
        private IBasicDataTypeService iBasic;
        private ComplexDataTypeColumnManagement mManagement;
        private int _DataTypeID;
        public ComplexDataTypeColumnModelService() : this(0,new ComplexDataTypeColumnService(), new ComplexDataTypeColumnMappingService(), new BasicDataTypeService()) { }
        public ComplexDataTypeColumnModelService(int ComplexDataTypeID, IComplexDataTypeColumnService Service, IComplexDataTypeColumnMappingService MappingService, IBasicDataTypeService BasicService)
        {
            iColumns = Service;
            iMapping = MappingService;
            iBasic = BasicService;
            mManagement = new ComplexDataTypeColumnManagement();
            DataTypeID = ComplexDataTypeID;
        }
        public int DataTypeID { get { return _DataTypeID; } set { _DataTypeID = value; GetList(_DataTypeID); } }
        public ComplexDataTypeColumnManagement GetList(int ComplexDataTypeID)
        {
            mManagement.Reset();
            mManagement.Items = new ComplexDataTypeColumnList();
            List<AddEditComplexDataTypeColumn> mComplexDataTypeColumnList = new List<AddEditComplexDataTypeColumn>();
            foreach (ComplexDataTypeColumnMapping mMapping in iMapping.All(ComplexDataTypeID).OrderBy(c=>c.ColumnOrder))
            {
                AddEditComplexDataTypeColumn mTemp = new AddEditComplexDataTypeColumn();
                ComplexDataTypeColumn mColumn = mMapping.ComplexDataTypeColumn;
                mTemp.BasicDataTypeID = mColumn.BasicDataTypeID;
                mTemp.BasicDataTypeSystemName = mColumn.BasicDataType.SystemName;
                mTemp.Comment = mColumn.Comment;
                mTemp.ID = mColumn.ID;
                mTemp.Name = mColumn.Name;
                mTemp.VariableName = mColumn.VariableName;
                mTemp.ColumnOrder = mMapping.ColumnOrder; 
                mComplexDataTypeColumnList.Add(mTemp);
            }
            mManagement.Items.List = mComplexDataTypeColumnList.ToList();
            mManagement.BasicDataType = GetBasicList();
            return mManagement;
        }
        private IEnumerable<SelectListItem> GetBasicList()
        {
            List<SelectListItem> mBasicList = new List<SelectListItem>();
            foreach (BasicDataType item in iBasic.All())
            {
                mBasicList.Add(new SelectListItem { Selected = false, Text = "[" + item.SystemName + "] " +  item.Name  , Value = item.ID.ToString() });
            }
            return mBasicList.ToList();
        }
        public ComplexDataTypeColumnManagement GetCreateModel()
        {
            mManagement.AddEditModel = new AddEditComplexDataTypeColumn();
            return mManagement;
        }
        public ComplexDataTypeColumnManagement GetCurrentModel(AddEditComplexDataTypeColumn Model)
        {
            mManagement.AddEditModel = Model;
            return mManagement;
        }
        public bool Create(AddEditComplexDataTypeColumn Model)
        {
            bool mStatus = false;
            int ID;
            mManagement.AddEditModel = Model;
            if(iColumns.Create(out ID, Model.BasicDataTypeID, Model.Name, Model.VariableName))
            {
                mStatus = iMapping.Create(Model.ColumnOrder, Model.ComplexDataTypeID, ID);
            }
            return mStatus;
        }

        private bool Create(out int ID, int BasicColumnTypeID, string Name, string VariableName, string Comment = "Created")
        {
            return iColumns.Create(out ID, BasicColumnTypeID, Name, VariableName, Comment);
        }
        public ComplexDataTypeColumnManagement GetUpdateModel(int ID)
        {

            mManagement.Items.SelectedIndex = ID;
            mManagement.AddEditModel = new AddEditComplexDataTypeColumn();
            ComplexDataTypeColumn mColumn = iColumns.Find(ID);
            if (mColumn != null)
            {
                
                mManagement.AddEditModel.AutoGenerateVaribleName = true;
                mManagement.AddEditModel.BasicDataTypeID = mColumn.BasicDataTypeID;
                mManagement.AddEditModel.Comment = mColumn.Comment;
                mManagement.AddEditModel.ID = mColumn.ID;
                mManagement.AddEditModel.Name = mColumn.Name;
                mManagement.AddEditModel.VariableName = mColumn.VariableName;
                mManagement.AddEditModel.BasicDataTypeSystemName = mColumn.BasicDataType.SystemName;
                //mManagement.AddEditModel.ComplexDataTypeID = mColumn.ComplexDataTypeColumnMappings.First(e => e.ComplexDataTypeColumnID == ID).ComplexDataTypeID;
            }
            //Imlement this
            return mManagement;
        }
        public bool Update(AddEditComplexDataTypeColumn Model)
        {
            bool mStatus = false;
            mManagement.Items.SelectedIndex = Model.ID;
            ComplexDataTypeColumn mColumn = iColumns.Find(Model.ID);
            if (mColumn != null)
            {
                mColumn.Name = Model.Name;
                mColumn.VariableName = Model.VariableName;
                mColumn.Comment = Model.Comment;
                iColumns.Update(mColumn, out mStatus);
            }
            return mStatus;
        }
        public bool Delete(int ID)
        {
            //Implement this

            //We also want to delete the mapping as well. 
            iMapping.DeleteColumnsByColumnID(ID);
            return iColumns.Delete(ID);
        }

        public bool DeleteColumns(int ComplexDataTypeID)
        {
            List<int> columns = new List<int>();
            foreach (ComplexDataTypeColumnMapping mMapping in iMapping.All(ComplexDataTypeID).OrderBy(c => c.ColumnOrder))
            {
                columns.Add(mMapping.ComplexDataTypeColumnID);
            }
            iMapping.DeleteColumns(ComplexDataTypeID);
            foreach (int ID in columns)
            {
                iColumns.Delete(ID);
            }
            return true;
        }
        public override Dictionary<string, string> GetErrors()
        {
            return iColumns.GetValidationErrors();
        }

        public bool UpdateColumnOrder(int ComplexDataTypeID, int ColumnID, int UpdatedOrder)
        {
            return iMapping.UpdateColumnOrder(ComplexDataTypeID, ColumnID, UpdatedOrder);
        }
    }
}
